Imports System.Xml
Imports ToDoList.Navigation

Namespace Usecases
  ''' <summary>
  ''' Usecase: user views the todolist
  ''' </summary>
  Public Class ManageTodoListUsecase

    Private mMyNavigation As IManageTodoListNavigation
    Private mEditUsecase As EditTaskDetailsUsecase

    'state
    Private mTodoList As Model.Todolist

    ''' <summary>
    ''' Create new usecase and insert the navigation 
    ''' object
    ''' </summary>
    ''' <param name="navigation">Navigation object which 
    ''' determines the view</param>
    ''' <param name="editUsecase">Dependent usecase</param>
    Public Sub New(ByVal navigation As IManageTodoListNavigation, ByVal editUsecase As EditTaskDetailsUsecase)
      mMyNavigation = navigation
      mEditUsecase = editUsecase
    End Sub

    ''' <summary>
    ''' Start the usecase to view the todolist:
    ''' open the list and navigate to the view
    ''' </summary>
    ''' <param name="todolist">the todolist model</param>
    Public Sub Start(ByVal todoList As Model.Todolist)

      'keep state
      mTodoList = todoList

      'delegate to navigation
      mMyNavigation.List(todoList, Nothing)
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="taskId"></param>
    ''' <remarks></remarks>
    Public Sub RequestDetails(ByVal taskId As Integer)
      Dim lTask As Model.Task = mTodoList.GetTask(taskId)

      'edit the task
      mEditUsecase.Start(lTask, mTodoList)

      If mEditUsecase.Result = EditTaskDetailsUsecase.EditResult.Success Then
        'refresh data
        mMyNavigation.List(mTodoList, lTask)
      End If
    End Sub

    ''' <summary>
    ''' User requests a new task on the same level as the context task
    ''' </summary>
    ''' <param name="contextTaskId">context of this command</param>
    Public Sub RequestNewTask(ByVal contextTaskId As Integer)
      createNewTask(contextTaskId, Model.Todolist.ContextRelation.Sibling)
    End Sub

    ''' <summary>
    ''' user requests a new task on child level of the context task
    ''' </summary>
    ''' <param name="contextTaskId"></param>
    ''' <remarks></remarks>
    Public Sub RequestSubTask(ByVal contextTaskId As Integer)
      createNewTask(contextTaskId, Model.Todolist.ContextRelation.Child)
    End Sub

    ''' <summary>
    ''' Create and edit a new task in a certain context
    ''' </summary>
    ''' <param name="contextTaskId">the context task</param>
    ''' <param name="contextRelation">the context specification</param>
    Private Sub createNewTask(ByVal contextTaskId As Integer, ByVal contextRelation As Model.Todolist.ContextRelation)
      'create new task as sibling of the contexttask
      Dim lNewTask As Model.Task = mTodoList.CreateNewTask( _
        mTodoList.GetTask(contextTaskId), contextRelation _
      )

      'edit the task
      mEditUsecase.Start(lNewTask, mTodoList)

      If mEditUsecase.Result = EditTaskDetailsUsecase.EditResult.Success Then
        'refresh data and select the item
        mMyNavigation.List(mTodoList, lNewTask)
      Else
        'remove added task from container
        Dim lParent As Model.TaskContainer = mTodoList.GetParentTask(lNewTask)
        If lParent Is Nothing Then
          lParent = mTodoList
        End If
        lParent.Remove(lNewTask)
      End If

    End Sub

    ''' <summary>
    ''' The state
    ''' </summary>
    Public ReadOnly Property TodoList() As Model.Todolist
      Get
        Return mTodoList
      End Get
    End Property
  End Class
End Namespace

